package com.example.demo.controller;


import com.example.demo.model.entity.TeacherEntity;
import com.example.demo.model.param.TeacherParam;
import com.example.demo.model.vo.ResponseVo;
import com.example.demo.service.TeacherService;
import com.example.demo.untils.BuildResponseUtils;
import com.example.demo.untils.PageBean;
import com.example.demo.untils.PageVoUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

@RestController
public class TeacherController {



    @Autowired
    private TeacherService teacherService;




    @PostMapping("/listTeacherInfo")
    public ResponseVo<?> listTeacherInfo(@RequestBody TeacherParam teacherParam){
        PageInfo pageInfo = teacherService.selectTeacherList(teacherParam);
        PageBean pageBean = PageVoUtils.convertTopageVo(pageInfo);
        return BuildResponseUtils.buildResponse(pageBean);
    }



    @PostMapping("/addTeacherInfo")
    public ResponseVo<?> addTeacherInfo(@RequestParam(required = false) MultipartFile file,String email,String name,String phone,String awards,String desc){
        if(!file.isEmpty()){
            try{
                if(file == null){
                    return BuildResponseUtils.buildResponse("请选择要上传的图片!");
                }
                if(file.getSize() > 1024 * 1024 * 10){
                    return BuildResponseUtils.buildResponse("文件大小不能大于10M!");
                }
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
                if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
                   BuildResponseUtils.buildResponse("请选择jpg,jpeg,gif,png格式的图片!");
                }
                String savePath = "d:\\testimg";
                File savePathFile = new File(savePath);
                if (!savePathFile.exists()) {
                    //若不存在该目录，则创建目录
                    savePathFile.mkdir();
                    System.out.println("我是不存在得！！！！！");
                }
                String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;
                try {
                    //将文件保存指定目录
                    file.transferTo(new File(savePath + "\\"+filename));
                } catch (Exception e) {
                    e.printStackTrace();
                    return BuildResponseUtils.buildResponse("我是不存在得！！！！！");
                }
                String dbpath = "http://localhost:8083/avatar/" + filename;
                TeacherEntity teacherEntity = new TeacherEntity();
                teacherEntity.setTeacherName(name);
                teacherEntity.setPhone(phone);
                teacherEntity.setEmail(email);
                teacherEntity.setImgUrl(dbpath);
                teacherEntity.setAwards(awards);
                teacherEntity.setDetails(desc);
                teacherService.save(teacherEntity);
            }catch (Exception e){
                return BuildResponseUtils.error();
            }
        }
        return BuildResponseUtils.buildResponse("创建成功!");
    }




    @PostMapping("/updateTeacherInfo")
    public ResponseVo<?> updateTeacherInfo(@RequestParam(required = false)MultipartFile file,Integer id,String email, String name, String phone, String awards, String desc){
        if(!file.isEmpty()){
            try{
                if(id == null){
                    return BuildResponseUtils.buildResponse("id为空");
                }
                if(file == null){
                    return BuildResponseUtils.buildResponse("请选择要上传的图片!");
                }
                if(file.getSize() > 1024 * 1024 * 10){
                    return BuildResponseUtils.buildResponse("文件大小不能大于10M!");
                }
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
                if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
                    BuildResponseUtils.buildResponse("请选择jpg,jpeg,gif,png格式的图片!");
                }
                String savePath = "d:\\testimg";
                File savePathFile = new File(savePath);
                if (!savePathFile.exists()) {
                    //若不存在该目录，则创建目录
                    savePathFile.mkdir();
                    System.out.println("我是不存在得！！！！！");
                }
                String filename = UUID.randomUUID().toString().replaceAll("-","") + "." + suffix;
                try {
                    //将文件保存指定目录
                    file.transferTo(new File(savePath + "\\"+filename));
                } catch (Exception e) {
                    e.printStackTrace();
                    return BuildResponseUtils.buildResponse("我是不存在得！！！！！");
                }
                String dbpath = "http://localhost:8083/avatar/" + filename;
                TeacherEntity teacherEntity = new TeacherEntity();
                teacherEntity.setId(id);
                teacherEntity.setTeacherName(name);
                teacherEntity.setPhone(phone);
                teacherEntity.setEmail(email);
                teacherEntity.setImgUrl(dbpath);
                teacherEntity.setAwards(awards);
                teacherEntity.setDetails(desc);
                teacherService.updateById(teacherEntity);
            }catch (Exception e){
                return BuildResponseUtils.error();
            }
        }
        return BuildResponseUtils.buildResponse("创建成功!");
    }



    @PostMapping("/deleteTeacherInfo")
    public ResponseVo<?> deleteTeacherInfo(@RequestBody TeacherEntity entity){
        return teacherService.removeById(entity.getId()) ? BuildResponseUtils.success():BuildResponseUtils.error();
    }




    @PostMapping("/getAllTeacherInfo")
    public ResponseVo<?> getAllTeacherInfo(){
        return BuildResponseUtils.buildResponse(teacherService.list());
    }

}
